#include "stdlib.h"
#include "fcntl.h"
#include "string.h"
#include <stdio.h>
#include <asm/io.h>
#include "hi_type.h"
#include "hi_defines.h"
#include "osal_mmz.h"
#include "hi_module_param.h"


#define SENSOR_NAME_LEN             64
#define CHIP_NAME_LEN               64
#define BOARD_NAME_LEN              64

#define himm(address, value) writel(value, address)
#define himd(address)        readl(address)

static unsigned long long mmz_start = 0x42000000;
static unsigned int       mmz_size  = 32;   /* MB, Synchronize modifying the mmz_size configuration in function: CHIP_init */
static HI_S32             g_chip_id = HI3516E_V200;
static char               chip_name[CHIP_NAME_LEN]     = "hi3516ev200";  /* hi3516ev200 hi3516ev300 hi3518ev300*/
static char               sensor_name[SENSOR_NAME_LEN] = "imx307";       /* imx307 imx327 imx335 sc4236 sc2235 sc2231 gc2053 */
static char               board_name[BOARD_NAME_LEN]   = "demo";         /* demo sck */

static HI_VOID CHIP_init(void)
{
    g_chip_id = himd(0x12020EE0);

    if(g_chip_id == HI3516E_V200)
    {
        snprintf(chip_name,   CHIP_NAME_LEN, "hi3516ev200");
        mmz_size = 32;
    }
}

static HI_S32 SYSCONFIG_init(void)
{
    extern int  hi_sysconfig_init(void);
    extern int  g_quick_start_flag;
    extern int  g_online_flag;
    extern int  g_cmos_yuv_flag;
    extern char sensor_list[SENSOR_NAME_LEN];
    extern char chip_list[CHIP_NAME_LEN];
    extern char board_list[BOARD_NAME_LEN];
    
    g_quick_start_flag = 0;
    g_online_flag      = 0;
    g_cmos_yuv_flag    = 0;
    strncpy(chip_list,   chip_name,   CHIP_NAME_LEN);
    strncpy(sensor_list, sensor_name, SENSOR_NAME_LEN);
    strncpy(board_list,  board_name,  BOARD_NAME_LEN);

    return hi_sysconfig_init();
}
extern void osal_proc_init(void);

static HI_S32 MMZ_init(void)
{
    extern int media_mem_init(void * pArgs);
    MMZ_MODULE_PARAMS_S stMMZ_Param = {0};

    snprintf(stMMZ_Param.mmz, MMZ_SETUP_CMDLINE_LEN, "anonymous,0,0x%llx,%dM", mmz_start, mmz_size);

    stMMZ_Param.anony = 1;

    dprintf("mmz_start=0x%llx, mmz_size=0x%x\n", mmz_start, mmz_size);
    dprintf("mmz param= %s\n", stMMZ_Param.mmz);

    return media_mem_init(&stMMZ_Param);
}
static HI_S32 BASE_init(void)
{
    extern void base_get_module_param(void *pArgs);
    extern int base_mod_init(void *pArgs);

    BASE_MODULE_PARAMS_S stBaseModuleParam;
    base_get_module_param(&stBaseModuleParam);

    return base_mod_init(&stBaseModuleParam);
}
void board_init()
{
    HI_S32 ret;
    CHIP_init();
    printf("chip init, chip name is %s, mmz_size is %dM. \n", chip_name, mmz_size);
    
    ret = SYSCONFIG_init();
    printf("sysconfig init,ret is %d. \n", ret);
    

    osal_proc_init();
    
    ret = MMZ_init();
    if (ret != 0)
    {
        printf("MMZ init error.\n");
    }
    ret = BASE_init();
    if (ret != 0)
    {
        printf("base init error.\n");
    }
}

